import os
import pandas as pd
import numpy as np
import time

def calculate_correlation_all():
    data_folder = 'src'
    flag = 0
    for file_name in os.listdir(data_folder):
        print file_name
        flag += 1
        path = data_folder + '/' + file_name
        table = pd.read_csv(path)
        print table.Id
        table = np.asarray(table.drop(['Id'],axis=1))
        for i in range(len(table)):
            if i == 0:
                V_d = table[i]
                V_ds = V_d
            elif i == 1:
                V_s = table[i]
                V_ss = V_s
            elif i>1:
                if i%2 == 0:
                    V_ds = np.hstack((V_ds,V_d))
                else:
                    V_ss = np.hstack((V_ss,V_s))
        if flag == 1:
            Vs_s = V_ss
            Vs_d = V_ds
        else:
            Vs_s = np.vstack((Vs_s,V_ss))
            Vs_d = np.vstack((Vs_d,V_ds))
    print flag
    print Vs_d,Vs_d.shape
    print Vs_s,Vs_s.shape
    print 'Vs_d: {0}'.format(np.corrcoef(Vs_d.transpose()))
    print 'Vs_s: {0}'.format(np.corrcoef(Vs_s.transpose()))

def combine_two_results():
    data_folder = 'src'
    value = 0
    for file_name in os.listdir(data_folder):
        print file_name
        path = data_folder + '/' + file_name
        table = pd.read_csv(path)
        Id = table.Id
        value += np.asarray(table.drop(['Id'],axis=1))
    print value.shape
    value = value/2.
    time.sleep(5)

    for i in range(600):
        print 'i: {0}'.format(i)
        P = 'P'+str(i)
        result = pd.DataFrame(columns=['Id',P])
        result['Id'] = Id
        result[P] = value.transpose()[i]
        #time.sleep(2)
        if i == 0:
            results = result
        else:
            results = pd.merge(results,result,on='Id')
    results.to_csv('results_bagging.csv',index=None,encoding='utf - 8')
combine_two_results()